//
//  ChatTableViewCell.m
//  HXSDK
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "ChatTableViewCell.h"
@interface ChatTableViewCell ()
/**时间*/
@property (nonatomic, strong) UILabel *timeLabel;
/**聊天信息*/
@property (nonatomic, strong) UIButton *chatBtn;
/**头像*/
@property (nonatomic, strong) UIButton *iconBtn;
/**姓名*/
@property (nonatomic, strong) UILabel *chatterLabel;
/**真实高度*/
@property (nonatomic, assign) CGSize realSize;
@end
@implementation ChatTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.timeLabel = [[UILabel alloc] init];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_timeLabel];
        
        self.chatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _chatBtn.backgroundColor = [UIColor magentaColor];
        [self addSubview:_chatBtn];
        
        self.iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_iconBtn];
        
        self.chatterLabel = [[UILabel alloc] init];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_chatterLabel];
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _timeLabel.frame = CGRectMake(WIDTH - 50, 0, 100, 20);
    
}
-(void)setMessage:(EMMessage *)message{
    _message = message;
    //获取消息体
    EMTextMessageBody *textBody = message.messageBodies[0];
    [self.chatBtn setTitle:textBody.text forState:UIControlStateNormal];
    _chatBtn.titleLabel.numberOfLines = 0;
    [self.iconBtn setImage:[UIImage imageNamed:@"12306.jpg"] forState:UIControlStateNormal];
    self.iconBtn.backgroundColor = [UIColor orangeColor];
   NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    self.realSize = [textBody.text boundingRectWithSize:CGSizeMake(WIDTH / 2,  MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
  NSString *chatter = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
     self.chatterLabel.text = chatter;
    //消息是谁发送的
    if ([message.from isEqualToString:chatter]) {//自己发送的
        //头像在右边
        _iconBtn.frame = CGRectMake(WIDTH - 60, 20, 50, 50);
        //聊天消息在左边
        _chatBtn.frame = CGRectMake(WIDTH - _realSize.width - 65, 40, _realSize.width, _realSize.height + 20);
        //名称在右边
        _chatterLabel.frame = CGRectMake(WIDTH - 100, 20, 50, 20);
        
    }else//别人发的, 头像在左边
    {
        //头像在左边
        _iconBtn.frame = CGRectMake(10, 20, 50, 50);
        //聊天消息在右边
        _chatBtn.frame = CGRectMake(70, 40, _realSize.width, _realSize.height + 20);
        //名称在左边
        _chatterLabel.frame = CGRectMake(55, 20, 50, 20);
    }
   

}
- (CGFloat) rowHeight{
    return 100 + _rowHeight;
}

@end
